//The contents of this file are subject to the Mozilla Public License Version 1.1
//(the "License"); you may not use this file except in compliance with the 
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
//
//Software distributed under the License is distributed on an "AS IS" basis,
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
//for the specific language governing rights and
//limitations under the License.
//
//The Original Code is "The Columba Project"
//
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003. 
//
//All Rights Reserved.

package org.columba.mail.mailchecking;

import java.awt.event.ActionEvent;

import javax.swing.Timer;

import org.columa.core.config.IDefaultItem;
import org.columba.core.config.DefaultItem;
import org.columba.core.connectionstate.ConnectionStateImpl;
import org.columba.core.gui.action.AbstractColumbaAction;
import org.columba.core.xml.XmlElement;
import org.columba.mail.config.AccountItem;

/**
 * For each account there exists one check action.
 * <p>
 * It keeps a reference to the check action, which is used by the
 * <b>File->Receive New Messages </b> menu.
 * 
 * @author fdietz
 */
public abstract class AbstractMailCheckingAction extends AbstractColumbaAction {
	private final static int ONE_SECOND = 1000;

	/**
	 * account item
	 */
	private AccountItem accountItem;
	private Timer timer;

	public AbstractMailCheckingAction(AccountItem accountItem) {
		super(null, null);

		this.accountItem = accountItem;

		createName();

		restartTimer();
	}

	private void createName() {
		//	generate label for menuitem
		String name = accountItem.getName();
		String address = accountItem.getIdentity().getAddress()
				.getMailAddress();
		String menuItemName = name + " (" + address + ")";

		putValue(AbstractColumbaAction.NAME, menuItemName);
	}

	public void restartTimer() {
		// recreate name of menuitem
		createName();

		IDefaultItem item = null;

		if (accountItem.isPopAccount()) {
			XmlElement e = accountItem.getRoot().getElement("popserver");
			item = new DefaultItem(e);
		} else {
			XmlElement e = accountItem.getRoot().getElement("imapserver");
			item = new DefaultItem(e);
		}

		if (item.getBoolean("enable_mailcheck")) {
			int interval = item.getIntegerWithDefault("mailcheck_interval", 10);

			timer = new Timer(ONE_SECOND * interval * 60, this);
			timer.restart();
		} else {
			if (timer != null) {
				timer.stop();
				timer = null;
			}
		}
	}

	/**
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	public void actionPerformed(ActionEvent arg0) {
		Object source = arg0.getSource();

		if (source.equals(timer)) {
			// timer action

			// only do checking if we are in online state
			if (ConnectionStateImpl.getInstance().isOnline())
				check();
			
		} else {
			// menuitem/toolbar button action
			check();
		}
	}

	/**
	 * Check for new messages.
	 * <p>
	 * Subclasses should implement this method.
	 *  
	 */
	public abstract void check();

	/**
	 * @return
	 */
	public AccountItem getAccountItem() {
		return accountItem;
	}

	/**
	 * @return
	 */
	public abstract boolean isCheckAll();
}